<?php 
/*
Extension Name: AntiSpam toolbox
Extension Url: http://getvanilla.com/
Description: Add a verification of the email to registration.
Version: 0.1.0
Author: Dinoboff
Author Url: dinoboff at hotmail dot com

Changelog:
  * Version 0.4.1
    * Fix a bug in the default role display on the registration page (Thanks to Alnokta for finding the bug)

  * Version 0.4.0:
  
    * Works only with vanilla 1.0.1 or higher. added a warning when the extension is enable on vanilla 1.0;
	* Use the new methode "SetDefinition()" to set the Dictionary entries. These entries won't overwrite the entries from the language packages or form conf/language.php;
	* You can add your own email template (email_Email_Verification.txt) to your language package or to the conf/ folder.
	  Applicant Email Verification will use in priority conf/email_Email_Verification.txt, languages/YOURLANGUAGE/email_Email_Verification.txt 
	  or extensions/ApplicantEmailVerification/conf/email_Email_Verification.txt.
*/

define('ANTI_SPAM_TOOLBOX_PATH', dirname(__FILE__) . '/');
define('ANTI_SPAM_TOOLBOX_TESTING', true);

// check if you have the right version of vanilla for this add-on
if (!version_compare(APPLICATION_VERSION, '1.0.1', '>=')) {
	//Language definitions for vanilla 1.0
	$Context->Dictionary['AntiSPamToolbox_ErrVanillaVersion'] = 'Vanilla 1.0.1 or higher is required for the Applicant Email Verification';
	
	//display an error message on the extension page and registration page when the extension is used with Vanilla 1.0.0
	if ( $Context->SelfUrl == 'settings.php' && in_array(ForceIncomingString('PostBackAction', ''), array('Extensions', 'RegistrationChange')) ) 
	$Context->WarningCollector->Add($Context->GetDefinition('AntiSPamToolbox_ErrVanillaVersion'));
}
else {

	require(ANTI_SPAM_TOOLBOX_PATH . 'library/Class.EmailVerification.php');
	require(ANTI_SPAM_TOOLBOX_PATH . 'library/Control.ApplicantEmailVerification.php');
	include(ANTI_SPAM_TOOLBOX_PATH . 'conf/language.php');
	require(ANTI_SPAM_TOOLBOX_PATH . 'conf/setting.php');

	
	if ( ($Context->SelfUrl == "people.php") && ($Context->Configuration['ANTI_SPAM_TOOLBOX_EMAIL_ON']) ) {
	
		//Hack: no mail sent to admin after registration, but sent after activation. Only work with vanilla 1.0.1 and higher
		function EmailVerification_NoMailAdmin(&$UserManager) {
			$UserManager->Context->Configuration['ALLOW_IMMEDIATE_ACCESS'] = 1;
		}	
		$Context->AddToDelegate('UserManager', 'PostRoleAssignment', 'EmailVerification_NoMailAdmin');
		
		function EmailVerification_SetBack_AllowImmediateAccess(&$ApplyForm) {
			$ApplyForm->Context->Configuration['ALLOW_IMMEDIATE_ACCESS'] = 0;
		}
		$Context->AddToDelegate('ApplyForm', 'PostCreateUser', 'EmailVerification_SetBack_AllowImmediateAccess');
		
		
		//set the message after application.
		if ($Context->Configuration['ANTI_SPAM_TOOLBOX_EMAIL_APROVE'])
			$Context->Dictionary['ApplicationWillBeReviewed'] = $Context->Dictionary['AntiSPamToolbox_MembershipAfterEmailVerification'];
		else $Context->Dictionary['ApplicationWillBeReviewed'] = $Context->Dictionary['AntiSPamToolbox_ApplicationReviewAfterVerification'];
	
		//Assign a verification key to the new applicant
		function RegistrationViaEmailCreateKey(&$ApplyForm) {
			if ($ApplyForm->PostBackValidated && $ApplyForm->Applicant->UserID > 0) {
							
				$User = $ApplyForm->Applicant->UserID;
				$EmailVerification = $ApplyForm->Context->ObjectFactory->NewContextObject($ApplyForm->Context, "EmailVerification", $ApplyForm->Applicant);
				
				//Create the key and save it in the DB
				$EmailVerification->CreateKey();
				
				//send the email
				if ($ApplyForm->Context->WarningCollector->Count() == 0) {
					
					$Subject = $ApplyForm->Context->Configuration['APPLICATION_TITLE'].' '.$ApplyForm->Context->GetDefinition('AntiSPamToolbox_email');
					$File = 'email_Email_Verification.txt';
					$DefaultPath = ANTI_SPAM_TOOLBOX_PATH . 'conf';
					$EmailVerification->MailKey($Subject, $File, $DefaultPath, 'people.php', 'ApplicantEmailVerification');
				}
			}
		}
		$Context->AddToDelegate('ApplyForm', 'PostCreateUser', 'RegistrationViaEmailCreateKey');
		
		
		//Check key and 
		//Create the new EmailVerification PostBackControl
		$ApplicantEmailVerification = $Context->ObjectFactory->NewContextObject($Context, 'ApplicantEmailVerification');
		$Page->AddRenderControl($ApplicantEmailVerification, $Configuration["CONTROL_POSITION_BODY_ITEM"]);
	}
	
	//Displayed the validation status of the applicant
	if ( ($Context->SelfUrl == "account.php") && ($Context->Configuration['ANTI_SPAM_TOOLBOX_EMAIL_ON']) ) {	
	
		function AccountProfil_EmailVerificationSatus(&$Account) {
			if($Account->Context->Session->User->Permission("PERMISSION_CHANGE_USER_ROLE") && $Account->User->RoleID == 0) {
				$status = '';
				$EmailVerification = $Account->Context->ObjectFactory->NewContextObject($Account->Context, "EmailVerification", $Account->User);
				if ($EmailVerification->GetKey()) $status = $Account->Context->GetDefinition('AntiSPamToolbox_EmailNotValidated');
				else $status = $Account->Context->GetDefinition('AntiSPamToolbox_EmailValidated');
				echo '		<li>
				<h3>'.$Account->Context->GetDefinition('AntiSPamToolbox_email').'</h3>
				<p>'.$status.'</p>
			</li>';
			}
		}
		$Context->AddToDelegate('Account', 'PostBasicPropertiesRender', 'AccountProfil_EmailVerificationSatus');
	}
	
	
	
	//Add the option to turn verification ON in the Registration page setting
	function RegistrationForm_EmailVerification(&$RegistrationForm) {
		$ConstantManager = &$RegistrationForm->DelegateParameters['ConstantManager'];
		
		$Verification = ForceIncomingBool('ANTI_SPAM_TOOLBOX_EMAIL_ON', 0);
		$ConstantManager->DefineSetting('ANTI_SPAM_TOOLBOX_EMAIL_ON', $Verification);
		
		if ($Verification) {
			$ConstantManager->DefineSetting('ANTI_SPAM_TOOLBOX_EMAIL_ROLE', $ConstantManager->_settings['DEFAULT_ROLE']);
			$ConstantManager->DefineSetting('DEFAULT_ROLE', 0);
			$ConstantManager->DefineSetting('ANTI_SPAM_TOOLBOX_EMAIL_APROVE', $ConstantManager->_settings['ALLOW_IMMEDIATE_ACCESS']);
			$ConstantManager->DefineSetting('ALLOW_IMMEDIATE_ACCESS', 0);
		}
	}
	$Context->AddToDelegate('RegistrationForm', 'PreSaveSettings', 'RegistrationForm_EmailVerification');
	
	function RegistrationForm_PreRender_EmailVerification(&$RegistrationForm) {
		$RoleID = ForceIncomingString('RoleID', '');
		if ($RoleID == '') $RoleID = $RegistrationForm->Context->Configuration['ANTI_SPAM_TOOLBOX_EMAIL_ROLE'];
		$RegistrationForm->RoleSelect->SelectedValue = $RoleID;	
	}
	$Context->AddToDelegate('RegistrationForm', 'PreRender', 'RegistrationForm_PreRender_EmailVerification');

		
	function RegistrationForm_Render_EmailVerification($RegistrationForm) {
		if ($RegistrationForm->PostBackAction == 'ProcessRegistrationChange') $checked = ForceIncomingBool('ANTI_SPAM_TOOLBOX_EMAIL_ON', 0);
		else $checked = $RegistrationForm->Context->Configuration['ANTI_SPAM_TOOLBOX_EMAIL_ON'];
		echo '							<li>
								<p><span>'.GetDynamicCheckBox('ANTI_SPAM_TOOLBOX_EMAIL_ON', 1, $checked, '', $RegistrationForm->Context->GetDefinition('AntiSPamToolbox_ActivationOption')).'</span></p>
								<p class="Description">'.$RegistrationForm->Context->GetDefinition('AntiSPamToolbox_ActivationOptionNote').'</p>
							</li>';
	}
	$Context->AddToDelegate('RegistrationForm', 'PostRegistrationOptions', 'RegistrationForm_Render_EmailVerification');
	
}
?>